package top.blackr.folivora.common.pay.transfer;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.utils.BeanUtil;
import net.dreamlu.mica.core.utils.JsonUtil;
import net.dreamlu.mica.core.utils.RandomType;
import org.springframework.beans.factory.annotation.Autowired;
import top.blackr.folivora.common.pay.function.TransferConsumer;
import top.blackr.folivora.common.pay.transfer.entity.TransferCallback;
import top.blackr.folivora.common.pay.transfer.entity.ali.AliTransferRequest;
import top.blackr.folivora.common.pay.transfer.entity.ali.AliTransferResponse;

/**
 * 微信提现默认实现，可直接继承该类
 *
 * @author BlackR
 */
@Getter
@Slf4j
public abstract class DefaultAliTransferStrategy implements ITransferStrategy<AliTransferRequest, AliTransferResponse, TransferCallback> {

    @Autowired
    private AlipayConfig alipayConfig;

    @Override
    public void generateNo(AliTransferRequest request) {
        if ($.isEmpty(request.getOutBatchNo())) {
            request.setOutBatchNo($.random(9, RandomType.ALL));
        }
    }

    @Override
    @SneakyThrows
    public void transfer(AliTransferRequest transferRequest, TransferConsumer<AliTransferResponse> consumer) {
        // 交易订单号
        generateNo(transferRequest);

        try {
            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
            model.setOutBizNo(transferRequest.getOutBatchNo());
            model.setRemark(transferRequest.getRemark());
            model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
            model.setBizScene("DIRECT_TRANSFER");
            Participant payeeInfo = new Participant();
            payeeInfo.setIdentity(transferRequest.getIdentity());
            payeeInfo.setIdentityType("ALIPAY_USER_ID");
            model.setPayeeInfo(payeeInfo);
            model.setTransAmount(String.valueOf(transferRequest.getTransferAmount()));
            model.setProductCode("TRANS_ACCOUNT_NO_PWD");
            model.setOrderTitle(transferRequest.getOrderTitle());
            request.setBizModel(model);
            // 状态码：https://opendocs.alipay.com/open/62987723_alipay.fund.trans.uni.transfer?scene=ca56bca529e64125a2786703c6192d41&pathHash=66064890#%E4%B8%9A%E5%8A%A1%E9%94%99%E8%AF%AF%E7%A0%81
            AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
            log.info("支付宝提现申请【{}】结果【{}】:{}", response.getOutBizNo(), response.getStatus(), JsonUtil.toJson(response));
            consumer.accept(BeanUtil.copy(response, AliTransferResponse.class));
        } catch (Exception e) {
            log.error("支付宝提现申请失败：{}", e.getMessage());
            throw e;
        }
    }

}
